package com.tuangou.tuangougoods.goods.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.tuangou.tuangougoods.goods.dao.GoodsDao;
import com.tuangou.tuangougoods.goods.entity.GoodsEntity;
import com.tuangou.tuangougoods.goods.service.GoodsService;


@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {

    @Autowired
    private GoodsService goodsService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                new QueryWrapper<GoodsEntity>()
        );
        return new PageUtils(page);
    }



    @Override
    public List<GoodsEntity> listgoods() {

        List<GoodsEntity> entities = baseMapper.selectList(null);

        return entities;
    }

    @Override
    public void updategoods(GoodsEntity goodsEntity) {
        this.updateById(goodsEntity);
        goodsService.updategoods(goodsEntity);
    }

    /*
    * 条件查询
    *
    * */

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {

        QueryWrapper<GoodsEntity> wrapper = new QueryWrapper<>();

        //根据商品名称
        String goodsname = (String) params.get("goodsname");
        if (!StringUtils.isEmpty(goodsname)){
            wrapper.eq("goodsname",goodsname);
        }
        // 和商品id查询
        String goodsid = (String) params.get("goodsid");
        if (!StringUtils.isEmpty(goodsid)){
            wrapper.eq("goodsid",goodsid);
        }

        //根据状态查询
        String status = (String) params.get("status");
        if(!StringUtils.isEmpty(status) && !status.equals(2)){
            wrapper.eq("status",status);
        }

        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils findsk(Map<String, Object> params) {

        QueryWrapper<GoodsEntity> wrapper = new QueryWrapper<>();

        Integer stock = (Integer) params.get("stock");
        if (stock==0){
            wrapper.eq("stock",stock);
        }
        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

}